package com.xfly.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ptmall.app.BuildConfig;
import com.ptmall.app.R;
import com.ptmall.app.base.BaseMvpActivity;
import com.ptmall.app.bean.model.db.Account;
import com.ptmall.app.net.HttpMethods;
import com.ptmall.app.net.api.ApiDataRepository;
import com.ptmall.app.net.base.ApiNetResponse;
import com.ptmall.app.utils.SharedPreferenceUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2019/9/15.
 */

public class XfLoginActivity extends BaseMvpActivity {

    Button lg_btn;
    EditText num,pass;
    TextView zc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_lg);
    }

    @Override
    public void initViews() {
        lg_btn = findViewById(R.id.lg_btn);
        num = findViewById(R.id.num);
        pass = findViewById(R.id.pass);
        zc = findViewById(R.id.zc);

        if(BuildConfig.DEBUG){
            num.setText("18805390539");
            pass.setText("123456000");
        }
    }

    @Override
    public void initEvents() {
        num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!TextUtils.isEmpty(pass.getText().toString())&&!TextUtils.isEmpty(num.getText().toString())){
                    lg_btn.setBackgroundResource(R.drawable.btn_red2);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        pass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(!TextUtils.isEmpty(pass.getText().toString())&&!TextUtils.isEmpty(num.getText().toString())){
                    lg_btn.setBackgroundResource(R.drawable.btn_red2);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(getActivity(), Web.class);
                intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/member/register");
                startActivity(intent);
            }
        });
        lg_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> map = new HashMap<>();
                map.put("account",num.getText().toString());
                map.put("password",pass.getText().toString());
                ApiDataRepository.getInstance().xfLogin(map, new ApiNetResponse<Object>(getContext()) {
                    @Override
                    public void onSuccess(Object data) {
                        SharedPreferenceUtil.setSharedStringData(getContext(),"token",data.toString());
                        Intent intent = new Intent(getActivity(), Web.class);
                        intent.putExtra("url", HttpMethods.BASE_SITE + "/cn/home/index/");
                        startActivity(intent);
                        finish();
                    }
                });
            }
        });
    }
}
